package org.arch_learn.spring_cloud.scn_homework.service_user.endpoint;

import org.arch_learn.spring_cloud.scn_homework.service_user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

@RequestMapping(value = "/user")
@RestController
public class UserEndpoint {
    @Autowired
    private IUserService userService;

    @GetMapping("/is-registered/{email}")
    public ResponseEntity<Boolean> isRegistered(@PathVariable @NotBlank String email) {
        Boolean result = userService.isRegistered(email);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/register/{email}/{password}/{code}")
    public ResponseEntity<Boolean> register(@PathVariable @NotBlank String email,
                                            @PathVariable @NotBlank String password,
                                            @PathVariable @NotBlank String code) {
        Boolean result = userService.register(email, password, code);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/login/{email}/{password}")
    public ResponseEntity<String> login(@PathVariable @NotBlank String email,
                                        @PathVariable @NotBlank String password,
                                        HttpServletResponse httpServletResponse
    ) {
        return userService.login(email, password, httpServletResponse);
    }
}
